package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.config.ThreadPoolExecutorConfig;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public Map<String, Object> getAlbumInfoById(Long albumId) {
        //  创建map集合;
        Map<String, Object> map = new HashMap<>();
        //  先判断布隆过滤器中是否有专辑Id
        //        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        //        //  判断
        //        if (!bloomFilter.contains(albumId)){
        //            //  抛出异常
        //            throw new GuiguException(809, "专辑不存在");
        //        }
        //  根据专辑Id获取专辑对象;
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //  远程调用获取数据;
            Result<AlbumInfo> infoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(infoResult, "专辑结果集不存在");
            AlbumInfo albumInfo = infoResult.getData();
            Assert.notNull(albumInfo, "专辑对象不存在");
            map.put("albumInfo", albumInfo);
            //  返回数据
            return albumInfo;
        }, threadPoolExecutor).exceptionally(throwable -> {
            //  异常信息；
            throwable.printStackTrace();
            //  出错了，返回空
            return null;
        });
        //  获取分类信息;
        CompletableFuture<Integer> categoryCompletableFuture = albumInfoCompletableFuture.thenApplyAsync(albumInfo -> {
            //  远程调用
            Result<BaseCategoryView> baseCategoryViewResult = categoryFeignClient.getBaseCategoryViewByCategoryId(albumInfo.getCategory3Id());
            Assert.notNull(baseCategoryViewResult, "视图结果集不存在！");
            BaseCategoryView baseCategoryView = baseCategoryViewResult.getData();
            Assert.notNull(baseCategoryView, "视图不存在！");
            //  返回数据
            map.put("baseCategoryView", baseCategoryView);
            //  返回数据;
            return 1;
        }, threadPoolExecutor).exceptionally(throwable -> {
            throwable.printStackTrace();
            return 0;
        });
        //  获取用户对象;
        CompletableFuture<Integer> userCompletableFuture = albumInfoCompletableFuture.thenApplyAsync(albumInfo -> {
            //  获取用户信息：
            Result<UserInfoVo> infoVoResult = userInfoFeignClient.getUserInfo(albumInfo.getUserId());
            Assert.notNull(infoVoResult, "用户信息结果集不存在！");
            UserInfoVo userInfoVo = infoVoResult.getData();
            //  返回数据;
            map.put("announcer", userInfoVo);
            return 1;
        }, threadPoolExecutor).exceptionally(throwable -> {
            throwable.printStackTrace();
            return 0;
        });
        //  存储数据; 根据专辑Id获取到专辑的统计信息;
        CompletableFuture<Integer> statCompletableFuture = albumInfoCompletableFuture.thenApplyAsync(albumInfo -> {
            //  远程调用;
            Result<AlbumStatVo> albumStatVoResult = albumInfoFeignClient.getAlbumStatVo(albumId);
            Assert.notNull(albumStatVoResult, "专辑统计结果集不存在！");
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            Assert.notNull(albumStatVo, "专辑统计不存在！");
            map.put("albumStatVo", albumStatVo);
            return 1;
        }, threadPoolExecutor).exceptionally(throwable -> {
            throwable.printStackTrace();
            return 0;
        });
        //  判断异步执行是否成功;
        try {
            if (null == albumInfoCompletableFuture.get() ||
                    categoryCompletableFuture.get() == 0 ||
                    userCompletableFuture.get() == 0 ||
                    statCompletableFuture.get() == 0){
                throw new GuiguException(809, "异步调用失败");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        //  多任务组合;
        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                categoryCompletableFuture,
                userCompletableFuture,
                statCompletableFuture
        ).join();
        //  返回map集合
        return map;
    }
}
